package com.ihr360.excel.aggregatemodule.aspect;

import com.ihr360.commons.exception.Ihr360SpreadSheetAsynException;
import com.ihr360.commons.exception.Ihr360SpreadSheetInterruptException;
import com.ihr360.excel.aggregatemodule.service.ExcelCloudService;
import com.ihr360.excel.vo.ProgressContext;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 流程中断后，拦截请求切面
 */
@Aspect
@Component
public class Ihr360SpreadSheetAsynExceptionAspect {


    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ExcelCloudService excelCloudService;

    @Pointcut("execution(* com.ihr360.excel.aggregatemodule.service..*(..))")
    public void allSpreadSheetProcessMethod() {

    }

    @AfterThrowing(pointcut = "allSpreadSheetProcessMethod()", throwing = "exception")
    public void handleInterruptMethod(Throwable exception) throws Throwable {
        if (exception instanceof Ihr360SpreadSheetAsynException) {
            Ihr360SpreadSheetAsynException spreadSheetAsynException = (Ihr360SpreadSheetAsynException) exception;
            ProgressContext progressContext = ProgressContext.getInstanceByProgressKey(spreadSheetAsynException.getProgressKey());
            excelCloudService.saveExcelProcessorJournal(progressContext, exception.getMessage());
        } else if (exception instanceof Ihr360SpreadSheetInterruptException) {
            logger.info(exception.getMessage());
        } else {
            throw exception;
        }
    }


}
